package program.conditions;

import attribute.*;
import be.kuleuven.cs.som.annotate.*;
import item.*;

/**
 * A class for dealing with energy-at-least-conditions: (energy-at-least ....).  
 * 
 * @invar       The energy amount associated with each energy-at-least-condition must
 *                      be a valid energy amount.
 *                      | canHaveAsEnergy(getEnergy())
 * 
 */
public class EnergyAtLeast extends Conditions {

        /**
         * Initialise this new energy-at-least-condition with a line number and energy amount.
         * 
         * @param       energy
         *                      The amount of energy associated with this condition (in Ws).
         * @pre         The given energy amount must be a valid energy amount for this condition.
         *                      | canHaveAsEnergy(energy)
         * @post        The given energy amount is set as the energy amount for this condition.
         *                      | new.getEnergy() == energy
         */
        public EnergyAtLeast(int lineNb, Energy energy) {
                super(lineNb);
                assert(canHaveAsEnergy(energy));
                this.energy = energy;
        }
        
        /**
         * Variable registering the amount of energy associated with this condition.
         */
        private final Energy energy;
        
        /**
         * Return the amount of energy associated with this condition.
         */
        @Basic @Raw @Immutable
        public Energy getEnergy() {
                return energy;
        }
        
        /**
         * Checks whether the given energy amount is a valid amount 
         * for an energy-at-least-condition.
         * 
         * @param       energy
         *                      The energy amount to check for whether it is a valid one.
         * @return      Return true if and only if and only if the given energy amount is effective;
         *                      else return false.
         *                      | result == ( energy != null )
         */
        @Raw
        public boolean canHaveAsEnergy(Energy energy) {
                return ( energy != null );
        }

        /**
         * Evaluates this condition for a given robot.
         * 
         * @return      
         * 			| result == ( super.evaluate(robot) && 
         *          |                     (robot.getEnergy().compareTo(getEnergy() >= 0))
         */
        @Override
        public boolean evaluate(Robot robot) {
                return (super.evaluate(robot) && (robot.getEnergy().compareTo(getEnergy()) >= 0) );
        }
        
        /**
         * Return a textual representation of this energy-at-least-condition, formatted in accordance with its position in the program.
         * 
         * @return
         *           | result == getIndentation(indentationLevel) + 
         *           |           "(energy-at-least: " + getEnergy() + getEnergy().getEnergyUnit() + ")"
         */
        @Override
        public String prettyFormat(int indentationLevel) {
                return getIndentation(indentationLevel) + 
                                "(energy-at-least: " + getEnergy() + getEnergy().getEnergyUnit() + ")";
        }

}